//
//  AfterApplyStep1.swift
//  smosky
//
//  Created by zhuxietong on 2018/8/13.
//  Copyright © 2018年 tianrun. All rights reserved.
//

import Foundation
import soapp
import Eelay
import AsyncDisplayKit




class AfterApplyStep1: FOneSelector<SaleAfter.SaleAfterTypeCell> {
    
    weak var controller:UIViewController? = nil
    
    var order = NSMutableDictionary()
    {
        didSet{
            self.mo.model = [
                "style":["tiny":___shop.tint],
                "options":SaleAfterType.validSeverFor(order: order).map({ (type) -> [String:String] in
                    return type.item
                })
                ].mutable_dictionary
        }
    }
    var serverType:SaleAfterType? = nil
    
    override func viewDidLoad() {
        title = "申请售后".loc
        buttonName = "下一步".loc
        showBottomButtton = true
        
        
        super.viewDidLoad()
        
        bar.bsui.set(radius: ThemeSet.buttonRadius, width: nil, color: nil)
        bar.setBackgroundImage(UIImage.gradient(colors: ThemeSet.Gradient.buy, size: [Swidth,38]), for: .normal)
        
        bar.ui.font(size: 14).ntitle(title: buttonName)
        bar.setTitle(buttonName, for: .normal)
        
        self.confirm = {[weak self] obj in
            self?.serverType = SaleAfterType(rawValue: obj["id",""])
        }
    }
    
    override func bottomAction() {
        guard let type = self.serverType else {
            "请选择售后类型".loc.alert()
            return
        }
        
        let ctr = AfterApplyStep2()
        ctr.mode = .order(order: order)
        ctr.serverType = type
        
//        controller?.navigationController?.pushViewController(ctr, animated: true)
//        co_delay(delay: <#T##Double#>, closure: <#T##() -> Void#>)
//
        let ctr1 = controller
        
//        JoClient.de
        JoClient.delay(delay: 0.2) {
            ctr1?.navigationController?.pushViewController(ctr, animated: true)
        }
        close()
//        
        
        //        weak var wself = self
        //        co_delay(delay: 0.2) {
        //            wself?.controller?.navigationController?.pushViewController(ctr, animated: true)
        //        }
    }
    
    
}

extension SaleAfter{
    
    class SaleAfterTypeCell: JoTableCell {
        let nameL = UILabel()
        let markL = UILabel()
        let selectIconV = UIImageView()
        let iconV = UIImageView()
        
        override func addLayoutRules() {
            contentView.eelay = [
                [iconV,"21",22,[ee.L.Y,[10,0]]],
                [nameL,[ee.T.L.R,[10,42,-60]]],
                
                [markL,[ee.L.R,[42,-60]],[nameL,ee.B,ee.T,6],[ee.B,-10]],
                
                [selectIconV,[ee.Y.R,[0,-10]],"20",20],
                [__line,[ee.L.B.R,[10,0,-10]],"0.5"]
            ]
            
            __line.backgroundColor = ._separator
            _ = nameL.ui.font14.text(color: ._foreground)
            _ = markL.ui.font12.cl_888
            contentView.backgroundColor = .clear
        }
        
        
        
        override func mo_model_reload() {
            contentView.backgroundColor = ._background
            nameL.text = mo.model["name",""]
            iconV.image = UIImage(named:mo.model["__raw__.icon",""])
            markL.text = mo.model["__raw__.desc",""]
            if mo.model["selected","NO"] == "YES"
            {
                selectIconV.image = UIImage(named:"radio_check_s")
            }
            else{
                selectIconV.image = UIImage(named:"radio_check_n")
            }
        }
    }
}





